{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "450645ae",
   "metadata": {},
   "source": [
    "# scipy基础使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "476ccaf2",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 导入库\n",
    "# 积分库\n",
    "import scipy.integrate as integrate\n",
    "# 特殊函数，\n",
    "import scipy.special as special"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "836c7142",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.0"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 试验区\n",
    "special.gamma(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "dcd24efd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on ufunc:\n",
      "\n",
      "ellipj = <ufunc 'ellipj'>\n",
      "    ellipj(x1, x2[, out1, out2, out3, out4], / [, out=(None, None, None, None)], *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])\n",
      "    \n",
      "    ellipj(u, m)\n",
      "    \n",
      "    Jacobian elliptic functions\n",
      "    \n",
      "    Calculates the Jacobian elliptic functions of parameter `m` between\n",
      "    0 and 1, and real argument `u`.\n",
      "    \n",
      "    Parameters\n",
      "    ----------\n",
      "    m : array_like\n",
      "        Parameter.\n",
      "    u : array_like\n",
      "        Argument.\n",
      "    \n",
      "    Returns\n",
      "    -------\n",
      "    sn, cn, dn, ph : ndarrays\n",
      "        The returned functions::\n",
      "    \n",
      "            sn(u|m), cn(u|m), dn(u|m)\n",
      "    \n",
      "        The value `ph` is such that if `u = ellipkinc(ph, m)`,\n",
      "        then `sn(u|m) = sin(ph)` and `cn(u|m) = cos(ph)`.\n",
      "    \n",
      "    Notes\n",
      "    -----\n",
      "    Wrapper for the Cephes [1]_ routine `ellpj`.\n",
      "    \n",
      "    These functions are periodic, with quarter-period on the real axis\n",
      "    equal to the complete elliptic integral `ellipk(m)`.\n",
      "    \n",
      "    Relation to incomplete elliptic integral: If `u = ellipkinc(phi,m)`, then\n",
      "    `sn(u|m) = sin(phi)`, and `cn(u|m) = cos(phi)`. The `phi` is called\n",
      "    the amplitude of `u`.\n",
      "    \n",
      "    Computation is by means of the arithmetic-geometric mean algorithm,\n",
      "    except when `m` is within 1e-9 of 0 or 1. In the latter case with `m`\n",
      "    close to 1, the approximation applies only for `phi < pi/2`.\n",
      "    \n",
      "    See also\n",
      "    --------\n",
      "    ellipk : Complete elliptic integral of the first kind\n",
      "    ellipkinc : Incomplete elliptic integral of the first kind\n",
      "    \n",
      "    References\n",
      "    ----------\n",
      "    .. [1] Cephes Mathematical Functions Library,\n",
      "           http://www.netlib.org/cephes/\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 帮助文档\n",
    "help(special.ellipj)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6798c66d",
   "metadata": {},
   "source": [
    "## 积分求解"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "ac602f8e",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\DELL\\AppData\\Local\\Temp/ipykernel_10416/2951393550.py:22: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  y = 1/x\n"
     ]
    }
   ],
   "source": [
    "### 导入库\n",
    "from scipy.integrate import quad, fixed_quad,cumtrapz\n",
    "from scipy import inf\n",
    "import numpy as np\n",
    "## 一重积分\n",
    "\n",
    "# 一般积分\n",
    "\n",
    "f1 = lambda x : x*x\n",
    "# 一般方式，返回计算值和误差，通常误差较小，当放大8个及以上的数量级时需要注意误差\n",
    "result1 = quad(f1, 0, 4)\n",
    "\n",
    "# 使用高斯正交积分，不返回误差，传入的参数n越大，计算更准确，也不一定\n",
    "result2 = fixed_quad(f1, 0, 4, n=5)\n",
    "\n",
    "# 无穷积分和瑕积分\n",
    "\n",
    "f2 = lambda x : 1/x\n",
    "result3 = quad(f2, 1e-10, 1)\n",
    "result4 = fixed_quad(f2, 1e-10, 1, n=5)\n",
    "x = np.linspace(0,1, 1000,endpoint=False)\n",
    "y = 1/x\n",
    "result5 = cumtrapz(y)\n",
    "\n",
    "# 二重积分\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "65d6cc5c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function fixed_quad in module scipy.integrate._quadrature:\n",
      "\n",
      "fixed_quad(func, a, b, args=(), n=5)\n",
      "    Compute a definite integral using fixed-order Gaussian quadrature.\n",
      "    \n",
      "    Integrate `func` from `a` to `b` using Gaussian quadrature of\n",
      "    order `n`.\n",
      "    \n",
      "    Parameters\n",
      "    ----------\n",
      "    func : callable\n",
      "        A Python function or method to integrate (must accept vector inputs).\n",
      "        If integrating a vector-valued function, the returned array must have\n",
      "        shape ``(..., len(x))``.\n",
      "    a : float\n",
      "        Lower limit of integration.\n",
      "    b : float\n",
      "        Upper limit of integration.\n",
      "    args : tuple, optional\n",
      "        Extra arguments to pass to function, if any.\n",
      "    n : int, optional\n",
      "        Order of quadrature integration. Default is 5.\n",
      "    \n",
      "    Returns\n",
      "    -------\n",
      "    val : float\n",
      "        Gaussian quadrature approximation to the integral\n",
      "    none : None\n",
      "        Statically returned value of None\n",
      "    \n",
      "    \n",
      "    See Also\n",
      "    --------\n",
      "    quad : adaptive quadrature using QUADPACK\n",
      "    dblquad : double integrals\n",
      "    tplquad : triple integrals\n",
      "    romberg : adaptive Romberg quadrature\n",
      "    quadrature : adaptive Gaussian quadrature\n",
      "    romb : integrators for sampled data\n",
      "    simpson : integrators for sampled data\n",
      "    cumulative_trapezoid : cumulative integration for sampled data\n",
      "    ode : ODE integrator\n",
      "    odeint : ODE integrator\n",
      "    \n",
      "    Examples\n",
      "    --------\n",
      "    >>> from scipy import integrate\n",
      "    >>> f = lambda x: x**8\n",
      "    >>> integrate.fixed_quad(f, 0.0, 1.0, n=4)\n",
      "    (0.1110884353741496, None)\n",
      "    >>> integrate.fixed_quad(f, 0.0, 1.0, n=5)\n",
      "    (0.11111111111111102, None)\n",
      "    >>> print(1/9.0)  # analytical result\n",
      "    0.1111111111111111\n",
      "    \n",
      "    >>> integrate.fixed_quad(np.cos, 0.0, np.pi/2, n=4)\n",
      "    (0.9999999771971152, None)\n",
      "    >>> integrate.fixed_quad(np.cos, 0.0, np.pi/2, n=5)\n",
      "    (1.000000000039565, None)\n",
      "    >>> np.sin(np.pi/2)-np.sin(0)  # analytical result\n",
      "    1.0\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(integrate.fixed_quad)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d05328d5",
   "metadata": {},
   "source": [
    "## 最值问题求解"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
